package cc.comac.mom.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipBenchmarkService;
import cc.comac.mom.bean.EquipBenchmark;
import cc.comac.mom.bean.EquipBenchmarkBean;
import cc.comac.mom.bean.EquipBenchmarkItem;
import cc.comac.mom.bean.EquipBenchmarkSelectBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.EquipBenchmarkDataHandler;
import cc.comac.mom.datahandlers.entity.EquipBenchmarkItemDataHandler;
import cc.comac.mom.entity.EquipBenchmarkItemEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.IModificationFlags;
import cc.comac.mom.util.ResultBean;
@RestController
@Service
public class EquipBenchmarkImpl implements EquipBenchmarkService{

	@Autowired
	private EquipBenchmarkDataHandler equipBenchmarkDataHandler;

	@Autowired
	private EquipBenchmarkItemDataHandler equipBenchmarkItemDataHandler;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Override
	public ResultBean save(@RequestBody EquipBenchmarkBean obj) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true);
		if(obj == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		List<EquipBenchmark> listEquipBenchmark = obj.getList();
		List<EquipBenchmarkItem> listEquipBenchmarkItem = obj.getListItem();
		if(listEquipBenchmark == null ||listEquipBenchmark.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		//标准工时
		BigDecimal decimal = new BigDecimal(0);	
		
		if(listEquipBenchmarkItem != null && listEquipBenchmarkItem.size()>0) {
			for(EquipBenchmarkItem item:listEquipBenchmarkItem) {
				if(item.getModificationFlag()==IModificationFlags.NEW) {
				if(item!= null && item.getInnerData() != null) {
					decimal = decimal.add(item.getInnerData().getBenchmarkTime());
				 }				
				}
			}
		}
		//判断主表是否执行新增
		long id =0;
		if(listEquipBenchmark.get(0).getModificationFlag() == IModificationFlags.NEW) {
			EquipBenchmark equipBenchmark = listEquipBenchmark.get(0);
			equipBenchmark.getInnerData().setStandardWorkTime(decimal);
//			equipBenchmark.getInnerData().setBenchmarkCode(idFactory.generateBaseNo());
			ResultBean resultBean2 = equipBenchmarkDataHandler.save(equipBenchmark);			
			if(!resultBean2.success) {
				return resultBean2;
			}
			id=Long.valueOf(resultBean2.getMessage());
		}
		//判断子项是否新增
		if(listEquipBenchmarkItem != null && listEquipBenchmarkItem.size()>0) {
			//ArrayList<EquipBenchmarkItemEntity> arrayList = new ArrayList<EquipBenchmarkItemEntity>();
			for(int i =0;i<listEquipBenchmarkItem.size();i++) {

				listEquipBenchmarkItem.get(i).getInnerData().setBenchmarkId(id);
				/*if(listEquipBenchmarkItem.get(i).getModificationFlag()==IModificationFlags.NEW) {
					//校验子项信息是否满足条件
					EquipBenchmarkItemEntity itemEntity = listEquipBenchmarkItem.get(i).getInnerData();	
					itemEntity.setBenchmarkId(id);
					ResultBean checkData = equipBenchmarkItemDataHandler.checkData(listEquipBenchmarkItem.get(i));
					if(!checkData.success) {
						throw new RuntimeException(checkData.getMessage());
					}	
					baseEntityDataUtil.setBaseEntityData(itemEntity);
					arrayList.add(itemEntity);
				}*/
				
			}
			ResultBean saveBatch = equipBenchmarkItemDataHandler.saveBatch(listEquipBenchmarkItem);
			if(!saveBatch.success) {
				throw new RuntimeException("保存失败");
			}
		}
		return resultBean;
	}

	@Override
	public ResultBean update(@RequestBody EquipBenchmark obj) {
		// TODO Auto-generated method stub
		return equipBenchmarkDataHandler.update(obj);
	}

	@Override
	public ResultBean deleteBatch(@RequestBody EquipBenchmarkBean obj) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true);
		List<EquipBenchmark> list = obj.getList();
		ResultBean deleteBatch = equipBenchmarkDataHandler.deleteBatch(list);
		if(!deleteBatch.success) {
			return deleteBatch;
		}
		ArrayList<EquipBenchmarkItem> arrayList = new ArrayList<EquipBenchmarkItem>();
		for(EquipBenchmark item:list) {
			List<EquipBenchmarkItem> list2 = equipBenchmarkItemDataHandler.getByEquipBenchmarkId(item.getInnerData().getId());
			if(list2 != null && list2.size()>0) {
				arrayList.addAll(list2);
			}
		}
		if(arrayList != null && arrayList.size()>0) {
			ResultBean resultBean2 = equipBenchmarkItemDataHandler.deleteBatch(arrayList);
			if(!resultBean2.success) {
				throw new RuntimeException(resultBean2.getMessage());
			}
		}
		return resultBean;
	}

	@Override
	public EquipBenchmark getById(@RequestBody EquipBenchmark obj) {
		return equipBenchmarkDataHandler.getById(obj.getInnerData().getId(),obj.getPageNum(),obj.getPageSize());
	}

	@Override
	public EquipBenchmarkBean selectByCondition(@RequestBody EquipBenchmark obj) {
		// TODO Auto-generated method stub
		return equipBenchmarkDataHandler.selectByCondition(obj);
	}

	@Override
	public ResultBean disableEquipBenchmark(@RequestBody EquipBenchmarkBean obj) {
		ResultBean resultBean = new ResultBean();
		// TODO Auto-generated method stub
		List<EquipBenchmark> list = obj.getList();
		if(list == null ||list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		ArrayList<Long> arrayList = new ArrayList<Long>();
		for(EquipBenchmark item :list) {
			if(item.getInnerData() == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;
			}
			arrayList.add(item.getInnerData().getId());
		}
		
		return equipBenchmarkDataHandler.disableEquipBenchmarkByIds(arrayList);
	}

	@Override
	public ResultDev selectByFields(@RequestBody EquipBenchmarkSelectBean obj) {
		// TODO Auto-generated method stub
		return equipBenchmarkDataHandler.selectByFields(obj);
	}

}
